/* DateTimeValue.cs -- 
   Copyright (C) 2008  Casey Marshall.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


using System;
using SexpSharp;

namespace SexpSharp.RPC
{
	/// <summary>
	/// An date/time value.
	/// </summary>
	public sealed class DateTimeValue : IValue
	{
		private DateTime _value;

		/// <summary>
		/// Constructs a new Integer using the given value.
		/// </summary>
		///
		/// <param name="value">The integer value.</param>
		public DateTimeValue(DateTime value)
		{
			this._value = value;
		}

		public DateTimeValue(Expression expr)
		{
			List l = expr as List;
			if (l == null)
				throw new ArgumentException("must be a list", "expr");
			if (l.Count != 2 || !(l.First is Atom) || !(l.Second is Atom))
				throw new ArgumentException("list must have exactly two atoms");
			if (l.First.ToString() != "date/time")
				throw new ArgumentException("first item of list must be 'date/time'");
			_value = ((Atom) l.Second).ToDateTime(null);
		}

		/// <summary>
		/// The underlying value.
		/// </summary>
		public DateTime Value
		{
			get { return _value; }
			set { _value = value; }
		}

		public Expression ToExpression()
		{
			List l = new List();
			l.Add(new Atom("date/time"));
			l.Add(new Atom(_value));
			return l;
		}

		public override string ToString()
		{
			return _value.ToString();
		}

		public bool Equals(IValue that)
		{
			if (that is DateTimeValue)
				return ((DateTimeValue) that)._value == _value;
			return false;
		}

		public override bool Equals(object o)
		{
			if (o is IValue)
				return Equals((IValue) o);
			return false;
		}

		public override int GetHashCode()
		{
			return _value.GetHashCode();
		}
	}
}
